package com.sdgakj.service.msg;

import com.google.common.collect.Maps;
import com.sdgakj.entity.MessageUser;
import com.sdgakj.enums.EnumIsDelete;
import com.sdgakj.repository.MessageUserDao;
import com.sdgakj.utils.MsgSearchFilter;
import com.sdgakj.utils.MsgSpecifications;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class MessageUserService {

	@Autowired
	private MessageUserDao messageUserDao;

	public MessageUser getMessageUser(Long id) {
		return messageUserDao.findOne(id);
	}

	public List<MessageUser> getMessageList() {
		return (List<MessageUser>) messageUserDao.findAll();
	}

	public MessageUser saveMessageUser(MessageUser entity) {
		return messageUserDao.save(entity);
	}

	public Iterable<MessageUser> saveList(List<MessageUser> list) {
		return messageUserDao.save(list);
	}

	public void updateMessageUser(MessageUser entity) {
		messageUserDao.save(entity);
	}

	public void deleteMessageUser(Long id) {
		MessageUser tempMessage = this.getMessageUser(id);
		tempMessage.setDeltag(EnumIsDelete.DELETED.getCode());
		this.saveMessageUser(tempMessage);
	}

	public List<MessageUser> getMessageAllByTag(String deltag) {
		return messageUserDao.findByTag(deltag);
	}

	public List<Long> getMessageUserByMidWithUid(String deltag, String uid) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("deltag", new SearchFilter("deltag", Operator.EQ, deltag));
		filters.put("customor.id", new SearchFilter("customor.id", Operator.EQ, uid));
		Specification<MessageUser> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				MessageUser.class);
		List<MessageUser> messageUserList = messageUserDao.findAll(spec);
		List<Long> ids = new ArrayList<Long>();
		for (MessageUser user : messageUserList) {
			ids.add(user.getMessage().getId());
		}
		return ids;
	}

	public Page<MessageUser> getMessage(Map<String, Object> searchParams, String device, int pageNumber, int pageSize,
												  String sortName, String sortType) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortName, sortType);
		Map<String, MsgSearchFilter> filters = MsgSearchFilter.parse(searchParams);
		if(!"4".equals(device)) {
			filters.put("Message.device", new MsgSearchFilter("Message.device", MsgSearchFilter.Operator.IN, device + ",3,4"));
		}
		Specification<MessageUser> spec = MsgSpecifications.bySearchFilter(filters.values(),
				MessageUser.class);
		return messageUserDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建排序.
	 */
	private Sort buildSortRequest(String sortName, String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		} else if ("desc".equals(sortType)) {
			sort = new Sort(Direction.DESC, sortName);
		} else {
			sort = new Sort(Direction.ASC, sortName);
		}
		return sort;
	}

	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortName, String sortType) {
		Sort sort = buildSortRequest(sortName, sortType);
		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}

	public List<MessageUser> getMessageAllByTag(String deltag, String sortName, String sortType) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if (StringUtils.isNotBlank(deltag)) {
			filters.put("deltag", new SearchFilter("deltag", Operator.EQ, deltag));
		}
		Specification<MessageUser> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				MessageUser.class);
		Sort sort = buildSortRequest(sortName, sortType);
		return messageUserDao.findAll(spec, sort);
	}

	public void deleteByMid(Long mid) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("Message.id", new SearchFilter("Message.id", Operator.EQ, mid));
		Specification<MessageUser> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				MessageUser.class);
		List<MessageUser> list = messageUserDao.findAll(spec);
		if (list != null && list.size() > 0) {
			for (MessageUser tmu : list) {
				tmu.setDeltag(EnumIsDelete.DELETED.getCode());
			}
			messageUserDao.save(list);
		}
	}

	public void updateRead(Long uid, Long mid) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("Message.id", new SearchFilter("Message.id", Operator.EQ, mid));
		filters.put("customor.id", new SearchFilter("customor.id", Operator.EQ, uid));
		Specification<MessageUser> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				MessageUser.class);
		MessageUser user = messageUserDao.findOne(spec);
		if (user != null) {
			user.setIsread("1");
			messageUserDao.save(user);
		}
	}

	public void updateReadById(Long id) {
		MessageUser user = this.getMessageUser(id);
		user.setIsread("1");
		this.saveMessageUser(user);
	}

	public void updateReadByIds(List<Long> ids) {
		List<MessageUser> list = (ArrayList<MessageUser>) messageUserDao.findAll(ids);
		for (MessageUser mes : list) {
			mes.setDeltag(EnumIsDelete.DELETED.getCode());
		}
		this.saveList(list);
	}

	public int getUnReadCount(Long uid) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("customor.id", new SearchFilter("customor.id", Operator.EQ, uid));
		filters.put("deltag", new SearchFilter("deltag", Operator.EQ, "0"));
		filters.put("isread", new SearchFilter("isread", Operator.EQ, "0"));
		Specification<MessageUser> spec = DynamicSpecifications.bySearchFilter(filters.values(),
				MessageUser.class);
		List<MessageUser> list = messageUserDao.findAll(spec);
		if (list != null && list.size() > 0) {
			return list.size();
		} else {
			return 0;
		}
	}

	public void deleteAll(List<Long> ids) {
		for(Long id:ids){
			this.messageUserDao.delete(id);
		}
	}

	public List<Long> getMessageUserByMidWithUidExceptDel(String uid, String device) {
		Map<String, MsgSearchFilter> filters = Maps.newHashMap();
		filters.put("customor.id", new MsgSearchFilter("customor.id", MsgSearchFilter.Operator.EQ, uid));
		if(!"4".equals(device)) {
			filters.put("Message.device", new MsgSearchFilter("Message.device", MsgSearchFilter.Operator.IN, device + ",4"));
		}
//		filters.put("Message.type", new SearchFilter("Message.type", Operator.EQ, "3"));
		Specification<MessageUser> spec = MsgSpecifications.bySearchFilter(filters.values(),MessageUser.class);
		List<MessageUser> messageUserList = messageUserDao.findAll(spec);
		List<Long> ids = new ArrayList<Long>();
		for (MessageUser user : messageUserList) {
			ids.add(user.getMessage().getId());
		}
		return ids;
	}

	public List<Long> getMessageUserByMidWithUidExceptDel2(String uid) {
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("customor.id", new SearchFilter("customor.id", Operator.EQ, uid));
		filters.put("Message.type", new SearchFilter("Message.type", Operator.EQ, "3"));
		Specification<MessageUser> spec = DynamicSpecifications.bySearchFilter(filters.values(),MessageUser.class);
		List<MessageUser> messageUserList = messageUserDao.findAll(spec);
		List<Long> ids = new ArrayList<Long>();
		for (MessageUser user : messageUserList) {
			ids.add(user.getMessage().getId());
		}
		return ids;
	}
}
